package com.shuzijun.leetcode.editor.cn;

import java.util.Deque;
import java.util.LinkedList;

public class RemoveAllAdjacentDuplicatesInString{
    public static void main(String[] args) {
         Solution solution = new RemoveAllAdjacentDuplicatesInString().new Solution();
         solution.removeDuplicates("abbaca");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String removeDuplicates(String s) {
        Deque<Character> deque = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (deque.isEmpty() || deque.peekLast() != c){
                deque.addLast(c);
            }else {
                deque.pollLast();
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!deque.isEmpty()){
            sb.append(deque.pollFirst());
        }

        return sb.toString();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}